<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-item">
        <span class="span">接口名称 </span>
        <el-input v-model="listQuery.apiName" placeholder="请输入接口名称" style="width: 200px" />
      </div>
      <div class="filter-item">
        <span class="span">调用者ip </span>
        <el-input v-model="listQuery.callerIp" placeholder="请输入调用者ip" style="width: 200px" />
      </div>
      <div class="filter-item">
        <span class="span">状态 </span>
        <el-select v-model="listQuery.status" clearable placeholder="请选择状态" style="width: 200px">
          <el-option v-for="item in dictList('is_success')" :key="'type' + item.label" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>

      <el-button v-waves class="filter-item"  icon="el-icon-refresh" @click="handleRest">{{ $t('table.rest') }}</el-button>
      <!--      <el-button v-waves v-permission="['marketapilog:MarketApiLog:add']" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">-->
      <!--        {{ $t('table.add') }}-->
      <!--      </el-button>-->
      <!--      <el-button v-waves v-permission="['marketapilog:MarketApiLog:delete']" class="filter-item" style="margin-left: 10px;" type="danger" icon="el-icon-delete" @click="handleBathDelete">-->
      <!--        {{ $t('table.delete') }}-->
      <!--      </el-button>-->
      <!--      <el-button v-waves v-permission="['marketapilog:MarketApiLog:import']" class="filter-item" type="primary" icon="el-icon-upload2" @click="handleImport">-->
      <!--        {{ $t('table.import') }}-->
      <!--      </el-button>-->
      <!--      <el-button v-waves v-permission="['marketapilog:MarketApiLog:export']" :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleExport">-->
      <!--        {{ $t('table.export') }}-->
      <!--      </el-button>-->
    </div>

    <el-table ref="table"  max-height="700" :key="tableKey" v-loading="listLoading" :data="list" element-loading-text="正在加载，请稍后" border fit highlight-current-row style="width: 100%;">

      <!--      <el-table-column type="selection" width="55" />-->
      <el-table-column label="序号" align="center" type="index" width="100" />
      <!--      <el-table-column align="center" label="拖拽" width="80">-->
      <!--        <template slot-scope="{}">-->
      <!--          <svg-icon class="drag-handler" icon-class="drag" />-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="接口名称" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.apiName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调用者ip" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.callerIp }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调用时间" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.callerDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.status | dictLabel('is_success') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" min-width="100" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="text" size="small" icon="el-icon-tickets" @click="handleUpdate(row)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <import ref="import" template-url="/marketapilog/MarketApiLog/template" import-url="/marketapilog/MarketApiLog/import" @refreshList="getList" />
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" :page-sizes="pageArray" @pagination="getList" />

    <market-api-log-form ref="form" @refreshList="getList" />

  </div>
</template>

<script>
import { deleteMarketApiLog, fetchMarketApiLogList, exportMarketApiLog } from '@/api/marketapilog/marketApiLog'
import Import from '@/components/Import/import'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import marketApiLogForm from './marketApiLogForm'
import Pagination from '@/components/Pagination'
import Sortable from 'sortablejs'

export default {
  name: 'MarketApiLogList',
  components: { marketApiLogForm, Pagination, Import },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      multipleSelection: [],
      downloadLoading: false,
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        apiName: undefined,
        callerIp: undefined,
        status: undefined,
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      },
    }
  },
  computed: {
    ids() {
      const ids = []
      this.multipleSelection.forEach((ele) => {
        ids.push(ele.id)
      })
      return ids.join(',')
    },
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchMarketApiLogList(this.listQuery).then((response) => {
        this.list = response.data.data
        this.total = response.data.total
        this.setSort()
        this.listLoading = false
      })
    },
    handleCreate() {
      this.$refs.form.handleCreate()
    },
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row.id)
    },
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteMarketApiLog(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleBathDelete() {
      if (this.ids === '') {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定删除所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteMarketApiLog(this.ids).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleImport() {
      this.$refs.import.show()
    },
    handleExport() {
      this.downloadLoading = true
      exportMarketApiLog(this.listQuery).then((response) => {
        this.downloadLoading = true
        if (response.data.code === 0) {
          import('@/vendor/Export2Excel').then((excel) => {
            excel.export_byte_to_excel(response.data.data.bytes, response.data.data.title)
            this.downloadLoading = false
          })
        } else {
          this.$message.error(response.data.msg)
        }
      })
    },
    setSort() {
      const el = this.$refs.table.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
      this.sortable = Sortable.create(el, {
        setData: function (dataTransfer) {
          // to avoid Firefox bug
          // Detail see : https://github.com/RubaXa/Sortable/issues/1012
          dataTransfer.setData('Text', '')
        },
        onEnd: (evt) => {
          const targetRow = this.list.splice(evt.oldIndex, 1)[0]
          this.list.splice(evt.newIndex, 0, targetRow)
          // TODO：这里完成自己的业务处理
        },
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    //重置
    handleRest() {
      this.listQuery = {
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      }
      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}
.table-expand {
  font-size: 0;
}
.table-expand label {
  width: 90px;
  color: #99a9bf;
}
.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
